<?php

namespace App\Http\Controllers\Goods;

use App\Models\Attr;
use App\Models\Category;
use App\Models\CategoryAttr;
use App\Models\Goods;
use App\Models\GoodsAttr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 获取所有商品数据
        $goods=Goods::orderBy('id','desc')->paginate(10);
        return view('goods.index',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 获取所有顶级分类的数据
        $categorys=Category::where('pid',0)->get();
        return view('goods.create',compact('categorys'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        dd($request->all());
        // 将数据存入商品表
        $goods= Goods::create($request->all());
        // 循环商品可用属性存入商品属性表
        foreach ($request->attrs as $k=>$v){
            GoodsAttr::create([
                'goods_id'=>$goods['id'],
                'attr_id'=>$v
            ]);
        }

        return redirect()->route('goods.goods.index')->with('success','添加商品成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function show(Goods $goods)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function edit(Goods $good)
    {
//        dd($goods);
        $good->getCategory($good['category_id']);
        // 找到顶级分类数据
        $categorys=Category::where('pid',0)->get();
        // 找到当前商品顶级分类的所有二级分类数据
        $secondCategorys=Category::where('pid',$good['category_id'][0])->get();
        // 找到当前商品二级分类的所有三级分类数据
        $thirdCategorys=Category::where('pid',$good['category_id'][1])->get();
        // 获取当前商品在商品属性表中所有的属性id
        $attrIds=GoodsAttr::where('goods_id',$good['id'])->pluck('attr_id');
        // 获取当前商品所属分类的三级分类可用属性数据
        $categoryAttrs = CategoryAttr::where('category_id',$good['category_id'][2])->pluck('attr_id');
        // 通过商品所属分类可用属性id找到属性数据
        $attrs = Attr::whereIn('id',$categoryAttrs)->get();
        return view('goods.edit',compact('good','categorys','secondCategorys','thirdCategorys','attrIds','attrs'))->with('success','编辑成功');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Goods $good)
    {
//        dd($request->all());
        if(isset($request['is_commend'])){
            $good->update($request->all());
        }else{
            $request['is_commend']=0;
            $good->update($request->all());
        }


        // 删除原商品包含的属性
        GoodsAttr::where('goods_id',$good['id'])->delete();
        // 循环商品可用属性存入商品属性表
        foreach ($request->attrs as $k=>$v){
            GoodsAttr::create([
                'goods_id'=>$good['id'],
                'attr_id'=>$v
            ]);
        }
        return redirect()->route('goods.goods.index')->with('success','编辑商品成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function destroy(Goods $good)
    {
        // 删除商品属性表中对应的数据
        GoodsAttr::where('goods_id',$good['id'])->delete();
        // 删除数据
        $good->delete();

        return back()->with('success','删除成功');
    }
}
